import redis,threading
import time
ip = '192.168.16.1'
connre = redis.Redis(ip)
connre.delete('dxdy')

type1 = [0,1]
type1_scale = [1,1]

type2 = [2]
type2_scale = [1]

all_effect = False
dyis0 = False


class move_remains():
    def __init__(self,s):
        self.r_x = 0
        self.r_y = 0
        self.scale = s
    def clear(self):
        self.r_x = 0
        self.r_y = 0

    def put(self,dx,dy):
        self.r_x += dx*self.scale
        self.r_y += dy*self.scale
        dx = round(self.r_x)
        dy = round(self.r_y)
        self.r_x = self.r_x-dx
        self.r_y = self.r_y-dy
        return dx,dy
move_y = 0
move_y_remains = 0
from pynput import keyboard,mouse
def on_click(x,y, button, pressed): 
    global move_y,all_effect
    move_y = 0 
    all_effect = False

mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()
def is_abnor(fno,type,code,value):
    global move_y,move_y_remains
    if type==2 and code in [0,1,8]:
        return False
    print(type,value,code)
    if type==1 and value==1:
        if code==273:            
            if move_y==0:
                move_y=2
            else:
                move_y=0
        if code==272:
            mouse.Controller().click(mouse.Button.left)            
            # if move_y==2:
            #     move_y=0
            # else:
            #     move_y=2

        if code==275:
#            move_y = 1            
            global all_effect
            all_effect = not all_effect

        if code==274:
            if move_y ==3:
                move_y = 1
            else:  
                move_y = 3

    print('move_y',move_y)
    return True

def change_d(dx2,dy2, fno,type,code,value):
    is_scroll = False
    dx = 0
    dy = 0
    if type==2 and code == 0:
        dx = value
    elif type==2 and code == 1:
        dy = value
        if dyis0:
            dy = 0
    elif type==2 and code==8:
        is_scroll = True
        dx = -value
    else:
        return dx2,dy2,is_scroll

    if fno in type1 and code!=8 and not all_effect:
        dx,dy = mm[fno].put(dx,dy)
    else:
        if move_y==0:
            dx,dy = mm[fno].put(0,dx)
        if move_y==2:
            dx,dy = mm[fno].put(dx,0)
        if move_y==1:
            dx,dy = mm[fno].put(dx,0)
            dy = dx*-1
        if move_y==3:
            dx,dy = mm[fno].put(dx,0)
            dy = dx
    return dx+dx2,dy+dy2,is_scroll

def get_bulk():
    global move_y
    is_scroll = False
    dx = 0
    dy = 0
    x = connre.blpop('dxdy')[1]
    x = str(x)
    x = x.replace("'",'').replace('b','')
    fno,type,code,value =[int(i) for i in str(x).split('#')]
    if is_abnor(fno,type,code,value):
        return dx,dy,fno,is_scroll
    dx,dy,is_scroll = change_d(dx,dy, fno,type,code,value)
    while True:
        a = connre.lpop('dxdy')
        if not a:
            break
        a = str(a)
        a = a.replace("'",'').replace('b','')
        fno2,type,code,value =[int(i) for i in str(x).split('#')]
        if fno2 != fno:
            connre.lpush('dxdy',a)
            return dx,dy,fno,is_scroll
        if is_abnor(fno,type,code,value):
            continue
            
        dx,dy,is_scroll = change_d(dx,dy, fno,type,code,value)
    return dx,dy,fno,is_scroll

last_time = 0

mm = {}
for s,i in zip(type1_scale,type1):
    mm[i] = move_remains(s)
for s,i in zip(type2_scale,type2):
    mm[i] = move_remains(s)
def do_one():
    t1 = time.time()
    dx,dy,fno,is_scroll = get_bulk()
    block_time = time.time()-t1
    if block_time>0.8:
        print('clear',time.time())
        mm[fno].clear()
    x,y = mouse.Controller().position
    if not is_scroll:
        if y+dy >750:
            dy = max(750-y,0)
        if x+dx>1360:
            dx = max(1360-x,0)
    mouse.Controller().move(dx,dy)

from pynput import keyboard,mouse
def work():
    while True:
        do_one()
import threading
t = threading.Thread(target=work)
t.setDaemon(True)
t.start()

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class my5(QWidget):
    def __init__(self):
        super().__init__()        
        self.tray = QSystemTrayIcon(self)
        self.tray.setToolTip('mouse_rec')             
        self.last_sm = 100      
        m = {
             0:'aero_ns_xl.cur',#精确定位
             1:'aero_nesw_xl.cur',#精确定位
             2:'aero_ew_xl.cur',#精确定位
             3:'aero_nwse_xl.cur',#精确定位2
             4:'lns.cur',
             5:'lnesw.cur',
             6:'lwe.cur',
             7:'lnwse.cur'
            }
        q = QPushButton('dyis0')        
        q.clicked.connect(self.onb)
        l = QHBoxLayout()
        l.addWidget(q)
        self.setLayout(l)
        self.iconm = {}
        self.show()
        for i,f in m.items():
            self.iconm[i] = QIcon(f) 
                
        
        self.tray.activated.connect(self.trayA)
        self.ont()
        self.tray.show()  
        t2 = QTimer(self)
        t2.timeout.connect(self.ont)
        t2.start(100)                
    def onb(self):
        global dyis0
        dyis0 = not dyis0

    def trayA(self):
        print('act')
        if self.isHidden():
            self.show()
        else:
            self.hide()

    def ont(self):
        move2 = move_y
        if all_effect:
            move2 = move_y+4
        if self.last_sm == move2:
            return
        self.last_sm = move2
        if self.last_sm in self.iconm.keys():
            self.setWindowIcon(self.iconm[self.last_sm])            
            self.tray.setIcon(self.iconm[self.last_sm])
        return

a = QApplication([])  
w = my5()
a.exec_()